#!/bin/bash


# replace path to app: /Users/cs/Desktop/Test
# replace name of app: test.app
# replace name of certificate: "Developer ID Application: Christian Schmitz Software GmbH"

# Special characters like spaces need to be escaped in paths. e.g. space character gets a backslash before the space.

cd /Users/cs/Desktop/Test/Test.app/Contents

rm -Rfv _CodeSignature

# fix the frameworks
# rm commands to make sure we recreate things correctly.
cd Frameworks/

# delete unneeded files
rm OLEWrapper*
rm libxalan-c.111.0.dylib
rm libxerces-c.dylib
rm libxalanMsg.111.0.dylib

cd DBEngine.framework
rm -Rfv _CodeSignature
rm DBEngine
rm Resources
rm Versions/Current
ln -s Versions/A/Resources Resources
ln -s Versions/A/DBEngine DBEngine
ln -s A Versions/Current
cd ..

cd FMEngine.framework
rm -Rfv _CodeSignature
rm FMEngine
rm Resources
rm Versions/Current
ln -s Versions/A/Resources Resources
ln -s Versions/A/FMEngine FMEngine
ln -s A Versions/Current
cd ..

cd FMWrapper.framework
rm -Rfv _CodeSignature
rm FMWrapper
rm Resources
rm Versions/Current
rm -rf Versions/A/Headers
ln -s Versions/A/Resources Resources
ln -s Versions/A/FMWrapper FMWrapper
ln -s A Versions/Current
cd ..

cd OmniORB4.framework
rm -Rfv _CodeSignature
rm OmniORB4
rm Resources
rm Versions/Current
ln -s Versions/A/Resources Resources
ln -s Versions/A/OmniORB4 OmniORB4
ln -s A Versions/Current
cd ..

cd OpenSSL.framework
rm -Rfv _CodeSignature
rm OpenSSL
rm Resources
rm Versions/Current
ln -s Versions/A/Resources Resources
ln -s Versions/A/OpenSSL OpenSSL
ln -s A Versions/Current
cd ..

cd Support.framework
rm -Rfv _CodeSignature
rm Resources
rm Support
rm Versions/Current
ln -s Versions/A/Resources Resources
ln -s Versions/A/Support Support
ln -s A Versions/Current
cd ..


# now we sign all the parts
cd /Users/cs/Desktop/Test/

# remove extended attributes, if present
xattr -cr test.app

codesign -f -vvvv -s "Developer ID Application: Christian Schmitz Software GmbH" test.app/Contents/Frameworks/DBEngine.framework/Versions/A
codesign -f -vvvv -s "Developer ID Application: Christian Schmitz Software GmbH" test.app/Contents/Frameworks/FMEngine.framework/Versions/A
codesign -f -vvvv -s "Developer ID Application: Christian Schmitz Software GmbH" test.app/Contents/Frameworks/FMWrapper.framework/Versions/A
codesign -f -vvvv -s "Developer ID Application: Christian Schmitz Software GmbH" test.app/Contents/Frameworks/OmniORB4.framework/Versions/A
codesign -f -vvvv -s "Developer ID Application: Christian Schmitz Software GmbH" test.app/Contents/Frameworks/OpenSSL.framework/Versions/A
codesign -f -vvvv -s "Developer ID Application: Christian Schmitz Software GmbH" test.app/Contents/Frameworks/Support.framework/Versions/A
codesign -f -vvvv -s "Developer ID Application: Christian Schmitz Software GmbH" test.app/Contents/Frameworks/*.dylib
codesign -f -vvvv -s "Developer ID Application: Christian Schmitz Software GmbH" test.app/Contents/XPCServices/*.xpc
codesign -f -vvvv -s "Developer ID Application: Christian Schmitz Software GmbH" test.app/Contents/MacOS/Runtime
codesign -f -vvvv -s "Developer ID Application: Christian Schmitz Software GmbH" test.app

# test it
codesign -v -v test.app
spctl -v -a test.app

